<?php

namespace App\Http\Controllers\Home;

use App\Http\Model\Article;
use App\Http\Model\Category;
use App\Http\Model\Link;
use App\Http\Model\Navs;
use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;

class IndexController extends Controller
{
    public function index()
    {
        //点击量最高的6篇文章(站长区域)
        $pics = Article::orderBy('art_view','desc')->take(6)->get();
        //分页图文列表
        $data = Article::orderBy('art_time','desc')->paginate(5);
        //友情链接
        $links = Link::orderBy('link_order','desc')->get();
        //网站配置项

        return view('Home.index',compact('data','links','pics'));
    }

    public function cate($id)
    {
        //当前分类的相关信息
        $cate = Category::find($id);
        //当前分类的文章信息
        $data = Article::where('art_cate',$id)->orderBy('art_time','desc')->paginate(4);
        //当前分类的子分类
        $submenu = Category::where('cate_pid',$id)->take(4)->get();
        //查看次数自增
        Category::where('cate_id',$id)->increment('cate_view');
        return view('Home.list',compact('cate','data','submenu'));
    }

    public function article($id)
    {
        $field = Article::Join('category','article.art_cate','=','category.cate_id')->where('art_id',$id)->first();
        $article['pre'] = Article::where('art_id','<',$id)->orderBy('art_id','desc')->first();
        $article['next'] = Article::where('art_id','>',$id)->orderBy('art_id','asc')->first();
        $data = Article::where('art_cate',$field->cate_id)->orderBy('art_id','desc')->take(6)->get();
        //查看次数自增
        Article::where('art_id',$id)->increment('art_view');
        return view('Home.new',compact('field','article','data'));
    }
}
